<template>
  <el-dialog
    title="字典数据添加"
    v-model="visible"
    top="10vh"
    width="600px"
    :before-close="handleClose"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
  >
    <el-form
      ref="dataFormRef"
      :model="dataForm"
      :rules="dataFormRules"
      label-position="right"
      label-width="120px"
      v-loading="loading"
    >
      <el-form-item label="字典标签" prop="dictLabel">
        <el-input v-model="dataForm.dictLabel"></el-input>
      </el-form-item>
      <el-form-item label="字典键值" prop="dictValue">
        <el-input v-model="dataForm.dictValue"></el-input>
      </el-form-item>
      <el-form-item label="描述" prop="description">
        <el-input type="textarea" v-model="dataForm.description"></el-input>
      </el-form-item>
      <el-form-item label="内容" prop="content">
        <el-input type="textarea" v-model="dataForm.content"></el-input>
      </el-form-item>
    </el-form>

    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="updateOrCreate">确定</el-button>
    </template>

  </el-dialog>
</template>

<script lang="ts" setup>
import { ref, watch, type PropType } from 'vue'
import { UPDATE_MODEL_EVENT } from 'cm/contants'

import { validPhone } from '@/utils/validate'
import { createDict, type ICreateOrUpdateDictData } from '@/api/dict-data'
import { ElMessage } from 'element-plus'

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true,
    default: false
  },
  dictType: {
    type: String,
    required: true,
    default: ''
  },
  dictId: {
    type: String,
    required: true,
    default: ''
  }
})

const emit = defineEmits([UPDATE_MODEL_EVENT, 'change'])

const visible = ref<boolean>(false)

const defaultForm = {
  dictType: '',
  dictId: '',
  dictLabel: '',
  dictValue: '',
  description: '',
  content: '',
}

const handleClose = () => {
  dataForm.value = {
    dictType: '',
    dictId: '',
    dictLabel: '',
    dictValue: '',
    description: '',
    content: '',
  }
  emit(UPDATE_MODEL_EVENT, false)
}
// 表单逻辑
const dataForm = ref<ICreateOrUpdateDictData>({ ...defaultForm })

const getDictKeys = () => {
  dataForm.value.dictType = props.dictType as string
  dataForm.value.dictId = props.dictId as string
}



const dataFormRules = {
  dictLabel: [{ required: true, message: '请输入字典标签', trigger: 'blur' }],
  dictValue: [{ required: true, message: '请输入字典键值', trigger: 'blur' }]
}

watch(() => props.modelValue,
  async () => {
    visible.value = props.modelValue;
    if (props.modelValue) {
      await getDictKeys()
    }
  }
)
const loading = ref<boolean>(false)

const dataFormRef = ref()
const updateOrCreate = async () => {
  dataFormRef.value.validate(async (valid: boolean) => {
    if (valid) {
      loading.value = true
      const res = await createDict(dataForm.value)
      loading.value = false
      if (res?.code === 200) {
        ElMessage({ type: 'success', message: '创建成功' })
        emit('change')
        handleClose()
      } else {
        ElMessage({ type: 'error', message: res?.msg || '网络异常，请稍后重试！' })
      }
    }
  })
}
</script>
